<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
    <div class="content">
        <div class="panel panel-default panel-intro">

            <div class="panel-body">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane fade active in" id="one">
                        <div class="widget-body no-padding">

                            <div class="bootstrap-table">
                                <div class="commonsearch-table ">
                                    <fieldset>
                                        <div class="row">
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="c-supplier_id" class="control-label col-xs-4">单据类型</label>
                                                <div class="col-xs-8">
                                                    <select name="" id="type" class="form-control">
                                                        <option value="1"   {if condition="$row.type eq 1"}selected {else /} {/if}>采购</option>
                                                        <option value="2" {if condition="$row.type eq 2"}selected {else /} {/if}>销售</option>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="c-supplier_id" class="control-label col-xs-4">供应商</label>
                                                <div class="col-xs-8">
                                                    <input id="c-supplier_id"  data-source="supplier/supplier/index" data-field="nickname" class="form-control selectpage" name="supplier_id" type="text" value="{$row.supplier_id}">
                                                </div>
                                            </div>
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="c-customer_id" class="control-label col-xs-4">单据编号</label>
                                                <div class="col-xs-8">
                                                    <input id="c-number" data-rule="required"   class="form-control c-number" name="number" type="text" value="{$row.source_number}" disabled>
                                                    <input    class="form-control c-number" name="number" type="hidden" value="{$row.source_number}" >
                                                </div>
                                            </div>
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="c-customer_id" class="control-label col-xs-4">客户</label>
                                                <div class="col-xs-8">
                                                    <input id="c-customer_id"    class="form-control " name="customer_id" type="text" value="{$company}" disabled>
                                                    <input id="" data-rule="required"  class="form-control c-customer_id" name="customer_id" type="hidden" value="{$row.customer_id}" >
                                                </div>
                                            </div>
<!--                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">-->
<!--                                                <label for="contacts" class="control-label col-xs-4">联系人</label>-->
<!--                                                <div class="col-xs-8">-->
<!--                                                    <input type="text" class="form-control" name="contacts" value="{$row.contacts}" placeholder="联系人" id="contacts" data-index="14">-->
<!--                                                </div>-->
<!--                                            </div>-->
<!--                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">-->
<!--                                                <label for="address" class="control-label col-xs-4">收货地址</label>-->
<!--                                                <div class="col-xs-8">-->
<!--                                                    <input type="text" class="form-control" name="address" value="{$row.address}" placeholder="收货地址" id="address" data-index="14">-->
<!--                                                </div>-->
<!--                                            </div>-->
<!--                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">-->
<!--                                                <label for="mobile" class="control-label col-xs-4">手机</label>-->
<!--                                                <div class="col-xs-8">-->
<!--                                                    <input type="text" class="form-control" name="mobile" value="{$row.mobile}" placeholder="手机" id="mobile" data-index="14">-->
<!--                                                </div>-->
<!--                                            </div>-->
<!--                                            <br>-->
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                <label for="explain" class="control-label col-xs-4">说明</label><div class="col-xs-8">
                                                <input type="text" class="form-control" name="explain" value="{$row.explain}" placeholder="说明" id="explain" data-index="13">
                                            </div>

                                            </div>
                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3" >
                                                <label for="allfare" class="control-label col-xs-4">运费</label>
                                                <div class="col-xs-8">
                                                <input type="number" class="form-control" name="allfare" value="{$row.allfare}" placeholder="运费" id="allfare" data-index="14" >
                                                </div>
                                            </div>

                                            <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3" >
                                                <label for="nowtax" class="control-label col-xs-4">当前税点</label>
                                                <div class="col-xs-8">
                                                    <input type="number" class="form-control "  name="nowtax" id="nowtax" value="{$row.nowtax}" placeholder="税点" data-index="14">
                                                </div>
                                            </div>

                                        </div>
                                    </fieldset>
                                </div>
                            </div>


<!--                            <div class="fixed-table-toolbar"><div class="bs-bars pull-left"><div id="toolbar" class="toolbar" style="position: relative;">-->
<!--                                <a href="javascript:;" class="btn btn-success btn-add " title="添加"><i class="fa fa-plus"></i> 选择订单</a>-->
<!--                            </div>-->
                            </div><div class="columns-right pull-right" style="margin-top:10px;margin-bottom:10px;">
                            </div>
                                <table id="table" class="table table-striped table-bordered table-hover table-nowrap"  data-operate-edit="1" data-operate-del="1" width="100%">
                                    <thead>
                                    <tr>
                                        <th style="text-align: center; vertical-align: middle; " data-field="id"><div class="th-inner ">Id</div></th>
                                        <!--                                <th style="text-align: center; vertical-align: middle; " data-field="createtime"><div class="th-inner ">出库仓库</div></th>-->
                                        <th style="text-align: center; vertical-align: middle; " ><div class="th-inner ">商品编号</div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="number"><div class="th-inner ">商品名称</div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="supplier_id"><div class="th-inner ">规格</div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="order_goods_total"><div class="th-inner ">销售数量</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="arrival_total"><div class="th-inner ">单价</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="order_price"><div class="th-inner ">含税单价</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="order_price"><div class="th-inner ">合计</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">差价数量</div><div class="fht-cell"></div></th>
                                        <!--                                <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">客户差价</div><div class="fht-cell"></div></th>-->
                                        <!--                                <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">客户税点</div><div class="fht-cell"></div></th>-->
                                        <!--                                <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">供应商差价</div><div class="fht-cell"></div></th>-->
                                        <!--                                <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">供应商税点</div><div class="fht-cell"></div></th>-->
                                        <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">当前单价</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">当前含税单价</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">税点</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="paid_in_price"><div class="th-inner ">差额</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="operate"><div class="th-inner ">备注</div><div class="fht-cell"></div></th>
                                        <th style="text-align: center; vertical-align: middle; " data-field="operate"><div class="th-inner ">操作</div><div class="fht-cell"></div></th>
                                    </tr>
                                    </thead>
                                    <tbody data-listidx="0" class="connnn">
                                        {volist name="list" id="vo"}
                                        <tr data-index="0">
                                            <td style="text-align: center; vertical-align: middle; "><input name="row[{$vo.goods_id}][goods_id]" value="{$vo.goods_id}" style="display: none" class="gid gids{$vo.goods_id}">{$vo.goods_id}</td>
                                            <td style="text-align: center; vertical-align: middle; ">{$vo.number}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="title">{$vo.title}</td>
                                            <td style="text-align: center; vertical-align: middle; ">{$vo.specs}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="goods_num">{$vo.goods_num}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="old_price">{$vo.price}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="old_htprice">{$vo.ht_price}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="sfhj">{$vo.sfhj}</td>
                                            <td style="text-align: center; vertical-align: middle; " class="xxgg">
                                            <input type="text" class="form-control diff_num" style="display: none" value="{$vo.diff_num}" onkeyup="this.value=this.value.replace(/\D/g,'')" name="row[{$vo.goods_id}][diff_num]">
                                            <span class="">{$vo.diff_num}</span>
                                             </td>
                                            <td style="text-align: center; vertical-align: middle; " class="xxgg">
                                                <input type="text" class="form-control diff_price1" style="display: none" value="{$vo.diff_price1}" onkeyup="onlynumber(this);" name="row[{$vo.goods_id}][diff_price1]">
                                                <span class="">{$vo.diff_price1}</span>
                                            </td>
                                            <td style="text-align: center; vertical-align: middle; " class="xxgg">
                                                <input type="text" class="form-control diff_price2" style="display: none" value="{$vo.diff_price2}" onkeyup="onlynumber(this);" name="row[{$vo.goods_id}][diff_price2]">
                                                <span class="">{$vo.diff_price2}</span>
                                            </td>
                                             <td style="text-align: center; vertical-align: middle; " class="">
                                                <input type="text" class="form-control  " style="display: none" value="{$vo.dtax}" onkeyup="checkTaxFloat(this)" name="row[{$vo.goods_id}][tax]">
                                                <span class="diff_tax">{$vo.dtax}</span>
                                             </td>
                                             <td style="text-align: center; vertical-align: middle; " class="">
                                                <input type="text" class="form-control  tax2" style="display: none" value="" onkeyup="checkTaxFloat(this)" name="row[{$vo.goods_id}][price_diff]">
                                                <span class="price_diff">0</span>
                                             </td>
                                            <td style="text-align: center; vertical-align: middle; " class="xxgg">
                                                <input type="text" class="form-control" style="display: none" value="{$vo.remark}" name="row[{$vo.goods_id}][remark]">
                                                <span class="tremark ">{$vo.remark}</span>
                                            </td>

                                            <td style="text-align: center; vertical-align: middle; ">
                                                 <a href="javascript:;" class="btn btn-xs btn-danger btn-delone" data-toggle="tooltip" title="" data-table-id="table" data-field-index="15" data-row-index="0" data-button-index="2" data-original-title="删除"><i class="fa fa-trash"></i></a>&nbsp;&nbsp;
                                            </td>
                                            </tr>
                                        {/volist}
                                    </tbody>
                                    <tbody class="tbfoot" data-listidx="0" style="display: ">
                                    <td style="text-align: center; vertical-align: middle; ">合计</td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; " ></td>
                                    <td style="text-align: center; vertical-align: middle; " class="allnum">0</td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; " ></td>
                                    <td style="text-align: center; vertical-align: middle; "class="alprice">0</td>
                                    <td style="text-align: center; vertical-align: middle; "class="alldiff">0</td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <!--                                <td style="text-align: center; vertical-align: middle; "></td>-->
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; "class="all_price_diff">0</td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    <td style="text-align: center; vertical-align: middle; "></td>
                                    </tbody>
                                </table>
                            </div>
                            <br>
                            <div class="widget-body no-padding">
                                <div class="bootstrap-table">
                                    <div class="commonsearch-table ">
                                        <fieldset>
                                            <div class="row">
                                                <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3" style="display: none">
                                                    <label for="discount" class="control-label col-xs-4">优惠</label>
                                                    <div class="col-xs-8">
                                                        <input type="text" class="form-control " name="discount" value="0" placeholder="优惠" id="discount" data-index="7" onkeyup="checkInputIntFloat(this);">
                                                    </div>
                                                </div>
                                                <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3" style="display: none">
                                                    <label for="c-discount_explain" class="control-label col-xs-4">优惠说明</label>
                                                    <div class="col-xs-8">
                                                        <input id="c-discount-explain"   class="form-control " name="discount_explain" type="text" value="" >
                                                    </div>
                                                </div>
                                                <div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">
                                                    <label for="c-actual_cost" class="control-label col-xs-4">总费用</label>
                                                    <div class="col-xs-8">
                                                        <input id="c-actual-cost" data-rule="required"  class="form-control actual_cost" name="actual-cost" type="text" value="0" disabled>
                                                        <input  data-rule="required"  class="form-control actual_cost" name="actual_cost" type="text" value="0" style="display: none">
                                                    </div>
                                                </div>
                                            </div>
                                        </fieldset>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <button type="submit" class="btn btn-success btn-embossed subpurchase">{:__('OK')}</button>
<!--            <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>-->
        </div>
    </div>
</form>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
    is_ht = 1;
    count()
    function onlynumber(ob) {
        if (!ob.value.match(/^[\+\-]?\d*?\.?\d*?$/)) ob.value = ob.t_value; else ob.t_value = ob.value; if (ob.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) ob.o_value = ob.value;
    }
    var is_check = 0;
    $('.subpurchase').click(function () {
        if(is_check == 0){
            var noticename = '';
            // if($('.actual_cost').val() <0){
            //     layer.msg('实际费用不能低于0')
            //     return false
            // }

            if($('.connnn').find("tr").length<1){
                layer.msg('没有选择商品')
            }
            $('.connnn').find("tr").each(function(){
                var anum = $(this).find('.requisition_num').val()
                if(anum < 1 ){
                    noticename += $(this).find('.title').html() +'&nbsp'
                }

            });

            // if(noticename != ''){
            //     layer.msg(noticename+'未设置数量？')
            //     return false;
            // }else{
            //
            // }
        }
    })
    $('tbody').on('click','.xxgg',function () {
        input_index = $('.xxgg').index(this)
        $(this).find('span').attr('style','display:none')
        width = $(this).width()*0.73
        if(width <50){
            width =50;
        }
        $(this).find('input').select();
        $(this).find('input').show()
        $(this).find('input').focus();
        $(this).find('input').attr('style','width:'+width+'px')

    })
    $('#nowtax').blur(function () {
        console.log($(this).val())
        $('.diff_tax').html($(this).val())
        $('.diff_tax').prev().val($(this).val())
        console.log( $('.diff_tax').parent().val())
    })

    $('.connnn').on('blur','input',function () {
        if($(this).val() == ''){
            layer.msg('不能为空')
            return false
        }
        if($(this).hasClass('diff_price2')){
            is_ht = 1;
            price = $(this).val()/(1+$('#nowtax').val()/100)
            // console.log(price)
            $(this).parents('tr').find('.diff_price1').val(price.toFixed(4))
            $(this).parents('tr').find('.diff_price1').next().html(price.toFixed(4))
        }
        if($(this).hasClass('diff_price1')){
            is_ht = 0;
            htprice = $(this).val()*(1+$('#nowtax').val()/100)
            $(this).parents('tr').find('.diff_price2').val(htprice.toFixed(4))
            $(this).parents('tr').find('.diff_price2').next().html(htprice.toFixed(4))
        }

        $(this).hide();
        $(this).next().attr('style','display:')
        $(this).next().html($(this).val())
        count()
    })

    //差价数量
    //当前单价
    //当前含税单价
    //税点
    function count() {
        allnum =0;
        alprice =0;
        alldiff =0;
        price_diff = 0

        $('.connnn').find("tr").each(function(){

            goods_num = $(this).find('.goods_num').html()
            allnum =allnum*1+ goods_num*1
            sale_price = $(this).find('.sfhj').html()
            alprice = alprice*1+ sale_price*1

            //--------------------------------------

            //差价数量
            diff_num = $(this).find('.diff_num').val()//数量
            alldiff= alldiff*1+diff_num*1




            diff_price1 = $(this).find('.diff_price1').val()//单价
            diff_price2 = $(this).find('.diff_price2').val()//含税单价
            old_htprice = $(this).find('.old_htprice').html()//含税单价
            old_price = $(this).find('.old_price').html()//含税单价

            old_tax = $('.old_tax').val()
            now_tax = $('#nowtax').val()

            if(is_ht == 1){
                old_diff = old_htprice*diff_num
                new_diff = diff_num*diff_price2
                diff_price = new_diff-old_diff
                $(this).find('.price_diff').prev().val(diff_price.toFixed(4))
                $(this).find('.price_diff').html(diff_price.toFixed(4))
            }else{
                old_diff = diff_num*old_htprice
                new_diff = diff_num*diff_price1*(1+now_tax/100)


                diff_price = new_diff - old_diff
                $(this).find('.price_diff').prev().val(diff_price.toFixed(4))
                $(this).find('.price_diff').html(diff_price.toFixed(4))
            }

            price_diff+=diff_price

        })
        price_diff = price_diff.toFixed(4)
        $('.allnum').html(allnum)
        $('.alprice').html(alprice.toFixed(4))
        $('.alldiff').html(alldiff)
        $('.all_price_diff').html(price_diff)
        $('.actual_cost').val(price_diff*1+$('#allfare').val()*1)
    }
    $('#allfare').blur(function () {
        count()
    })
    $('.connnn').on('click','.btn-delone',function () {
        $(this).parents('tr').remove()
        count()
    })
    function checkTaxFloat(obj) {
        obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数
        if(obj.value >13){
            obj.value = 13;
        }
        var x = obj.value.indexOf('.') + 1;   //小数点的位置
        var y = obj.value.length;

        if(x>1 && (y-x) >0){
            if((y-x)>1){
                obj.value= parseFloat(obj.value).toFixed(1)
            }
        }
        if(obj.value.indexOf(".")< 0 && obj.value !=""){
            obj.value= parseFloat(obj.value)
        }
    }
</script>
